package com.wanmait.zaisousuo.controller;

import com.wanmait.zaisousuo.pojo.Admin;
import com.wanmait.zaisousuo.pojo.AdminLog;
import com.wanmait.zaisousuo.service.AdminLogService;
import com.wanmait.zaisousuo.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/backstage/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private AdminLogService adminLogService;

    //去往日志页面,//根据起止时间查询管理员的操作日志
    @RequestMapping("log")
    public ModelAndView log(Integer adminId,String startTime,String endTime){
        if(startTime=="")startTime=null;
        if(endTime=="")endTime=null;
        ModelAndView mv = new ModelAndView();
        mv.addObject("logs",adminLogService.findByTimeAndAdmin(adminId,startTime,endTime));
        mv.addObject("admins",adminService.findAll());
        return mv;
    }

    //去往管理员列表页面
    @RequestMapping("list")
    @ModelAttribute("admins")
    public List<Admin> list(){
        return adminService.findAll();
    }

    //去往添加页面
    @RequestMapping("addupdate")
    public void addupdate(){}


    //禁用/启用管理员
    @RequestMapping("changeDisabled")
    @ResponseBody
    public void changeDisabled(Boolean flag, Integer adminId, String adminName){
        adminService.changeDisabled(flag,adminId,adminName);
    }

    //添加管理员
    @RequestMapping("addAdmin")
    @ResponseBody
    public Integer addAdmin(Admin admin){
        return adminService.addAdmin(admin);
    }

    //清空管理员日志
    @RequestMapping("truncateAllLogs")
    @ResponseBody
    public void truncateAllLogs(){
        adminLogService.truncateAllLogs();
    }


    //更改管理员密码
    @RequestMapping("changeAdminPassword")
    @ResponseBody
    public void changeAdminPassword(Integer id,String newPW){
        adminService.changeAdminPassword(id,newPW);
    }




}
